16 #ifdef __MSP430_HAS_EUSCI_Bx__
21 void EUSCI_B_I2C_initMaster (uint16_t baseAddress,
22 EUSCI_B_I2C_initMasterParam *
param
25 uint16_t preScalarValue;
28 HWREG16(baseAddress + OFS_UCBxCTLW0) = UCSWRST;
31 HWREG16(baseAddress + OFS_UCBxCTLW1) &= ~UCASTP_3;
32 HWREG16(baseAddress + OFS_UCBxCTLW1) |= (uint16_t)
param->autoSTOPGeneration;
35 HWREG16(baseAddress + OFS_UCBxTBCNT) =
param->byteCounterThreshold;
42 HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCMST + UCMODE_3 + UCSYNC;
45 HWREG16(baseAddress + OFS_UCBxCTLW0) |= (
param->selectClockSource + UCSWRST);
53 preScalarValue = (uint16_t)(
param->i2cClk /
param->dataRate);
54 HWREG16(baseAddress + OFS_UCBxBRW) = preScalarValue;
57 void EUSCI_B_I2C_initSlave (uint16_t baseAddress,
58 EUSCI_B_I2C_initSlaveParam *
param
62 HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCSWRST;
65 HWREG16(baseAddress + OFS_UCBxCTLW0) &= ~UCMST;
68 HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCMODE_3 + UCSYNC;
71 HWREG16(baseAddress + OFS_UCBxI2COA0 +
param->slaveAddressOffset)
72 =
param->slaveAddress +
param->slaveOwnAddressEnable;
75 void EUSCI_B_I2C_enable (uint16_t baseAddress)
78 HWREG16(baseAddress + OFS_UCBxCTLW0) &= ~(UCSWRST);
81 void EUSCI_B_I2C_disable (uint16_t baseAddress)
84 HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCSWRST;
87 void EUSCI_B_I2C_setSlaveAddress (uint16_t baseAddress,
92 HWREG16(baseAddress + OFS_UCBxI2CSA) = (slaveAddress);
95 void EUSCI_B_I2C_setMode (uint16_t baseAddress,
99 HWREG16(baseAddress + OFS_UCBxCTLW0) &= ~EUSCI_B_I2C_TRANSMIT_MODE;
100 HWREG16(baseAddress + OFS_UCBxCTLW0) |= mode;
103 uint8_t EUSCI_B_I2C_getMode (uint16_t baseAddress)
106 return ((
HWREG16(baseAddress + OFS_UCBxCTLW0) & UCTR));
110 void EUSCI_B_I2C_slavePutData (uint16_t baseAddress,
118 uint8_t EUSCI_B_I2C_slaveGetData (uint16_t baseAddress)
121 return (
HWREG16(baseAddress + OFS_UCBxRXBUF));
124 uint16_t EUSCI_B_I2C_isBusBusy (uint16_t baseAddress)
127 return (
HWREG16(baseAddress + OFS_UCBxSTATW) & UCBBUSY);
130 uint16_t EUSCI_B_I2C_masterIsStopSent (uint16_t baseAddress)
132 return (
HWREG16(baseAddress + OFS_UCBxCTLW0) & UCTXSTP);
135 uint16_t EUSCI_B_I2C_masterIsStartSent (uint16_t baseAddress)
137 return (
HWREG16(baseAddress + OFS_UCBxCTLW0) & UCTXSTT);
140 void EUSCI_B_I2C_enableInterrupt (uint16_t baseAddress,
145 HWREG16(baseAddress + OFS_UCBxIE) |= mask;
148 void EUSCI_B_I2C_disableInterrupt (uint16_t baseAddress,
153 HWREG16(baseAddress + OFS_UCBxIE) &= ~(mask);
156 void EUSCI_B_I2C_clearInterrupt (uint16_t baseAddress,
161 HWREG16(baseAddress + OFS_UCBxIFG) &= ~(mask);
164 uint16_t EUSCI_B_I2C_getInterruptStatus (uint16_t baseAddress,
169 return (
HWREG16(baseAddress + OFS_UCBxIFG) & mask);
172 void EUSCI_B_I2C_masterSendSingleByte (uint16_t baseAddress,
177 uint16_t txieStatus =
HWREG16(baseAddress + OFS_UCBxIE) & UCTXIE;
180 HWREG16(baseAddress + OFS_UCBxIE) &= ~(UCTXIE);
183 HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTR + UCTXSTT;
186 while (!(
HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG)) ;
189 HWREG16(baseAddress + OFS_UCBxTXBUF) = txData;
192 while (!(
HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG)) ;
195 HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTXSTP;
198 HWREG16(baseAddress + OFS_UCBxIFG) &= ~(UCTXIFG);
201 HWREG16(baseAddress + OFS_UCBxIE) |= txieStatus;
204 uint8_t EUSCI_B_I2C_masterReceiveSingleByte (uint16_t baseAddress)
207 HWREG16(baseAddress + OFS_UCBxCTLW0) &= ~UCTR;
210 HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTXSTT;
213 while(
HWREG16(baseAddress + OFS_UCBxCTLW0) & UCTXSTT);
216 HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTXSTP;
219 while (!(
HWREG16(baseAddress + OFS_UCBxIFG) & UCRXIFG));
222 return (
HWREG16(baseAddress + OFS_UCBxRXBUF));
225 bool EUSCI_B_I2C_masterSendSingleByteWithTimeout (uint16_t baseAddress,
231 uint32_t timeout2 = timeout;
234 uint16_t txieStatus =
HWREG16(baseAddress + OFS_UCBxIE) & UCTXIE;
237 HWREG16(baseAddress + OFS_UCBxIE) &= ~(UCTXIE);
240 HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTR + UCTXSTT;
243 while ((!(
HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG)) && --timeout);
251 HWREG16(baseAddress + OFS_UCBxTXBUF) = txData;
254 while ((!(
HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG)) && --timeout2);
262 HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTXSTP;
265 HWREG16(baseAddress + OFS_UCBxIFG) &= ~(UCTXIFG);
268 HWREG16(baseAddress + OFS_UCBxIE) |= txieStatus;
273 void EUSCI_B_I2C_masterSendMultiByteStart (uint16_t baseAddress,
278 uint16_t txieStatus =
HWREG16(baseAddress + OFS_UCBxIE) & UCTXIE;
281 HWREG16(baseAddress + OFS_UCBxIE) &= ~(UCTXIE);
284 HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTR + UCTXSTT;
287 while (!(
HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG)) ;
290 HWREG16(baseAddress + OFS_UCBxTXBUF) = txData;
293 HWREG16(baseAddress + OFS_UCBxIE) |= txieStatus;
296 bool EUSCI_B_I2C_masterSendMultiByteStartWithTimeout (uint16_t baseAddress,
302 uint16_t txieStatus =
HWREG16(baseAddress + OFS_UCBxIE) & UCTXIE;
305 HWREG16(baseAddress + OFS_UCBxIE) &= ~(UCTXIE);
308 HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTR + UCTXSTT;
311 while ((!(
HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG)) && --timeout);
319 HWREG16(baseAddress + OFS_UCBxTXBUF) = txData;
322 HWREG16(baseAddress + OFS_UCBxIE) |= txieStatus;
327 void EUSCI_B_I2C_masterSendMultiByteNext (uint16_t baseAddress,
332 if (!(
HWREG16(baseAddress + OFS_UCBxIE) & UCTXIE)){
334 while (!(
HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG)) ;
338 HWREG16(baseAddress + OFS_UCBxTXBUF) = txData;
341 bool EUSCI_B_I2C_masterSendMultiByteNextWithTimeout (uint16_t baseAddress,
347 if (!(
HWREG16(baseAddress + OFS_UCBxIE) & UCTXIE)){
349 while ((!(
HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG)) && --timeout);
358 HWREG16(baseAddress + OFS_UCBxTXBUF) = txData;
363 void EUSCI_B_I2C_masterSendMultiByteFinish (uint16_t baseAddress,
368 if (!(
HWREG16(baseAddress + OFS_UCBxIE) & UCTXIE)){
370 while (!(
HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG)) ;
374 HWREG16(baseAddress + OFS_UCBxTXBUF) = txData;
377 while (!(
HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG)) ;
380 HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTXSTP;
383 bool EUSCI_B_I2C_masterSendMultiByteFinishWithTimeout (uint16_t baseAddress,
388 uint32_t timeout2 = timeout;
391 if (!(
HWREG16(baseAddress + OFS_UCBxIE) & UCTXIE)){
393 while ((!(
HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG)) && --timeout) ;
402 HWREG16(baseAddress + OFS_UCBxTXBUF) = txData;
405 while ((!(
HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG)) && --timeout2) ;
413 HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTXSTP;
418 void EUSCI_B_I2C_masterSendStart (uint16_t baseAddress)
420 HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTXSTT;
423 void EUSCI_B_I2C_masterSendMultiByteStop (uint16_t baseAddress)
426 if (!(
HWREG16(baseAddress + OFS_UCBxIE) & UCTXIE)){
428 while (!(
HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG)) ;
432 HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTXSTP;
435 bool EUSCI_B_I2C_masterSendMultiByteStopWithTimeout (uint16_t baseAddress,
439 if (!(
HWREG16(baseAddress + OFS_UCBxIE) & UCTXIE)){
441 while ((!(
HWREG16(baseAddress + OFS_UCBxIFG) & UCTXIFG)) && --timeout) ;
450 HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTXSTP;
455 void EUSCI_B_I2C_masterReceiveStart (uint16_t baseAddress)
458 HWREG16(baseAddress + OFS_UCBxCTLW0) &= ~UCTR;
460 HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTXSTT;
463 uint8_t EUSCI_B_I2C_masterReceiveMultiByteNext (uint16_t baseAddress)
465 return (
HWREG16(baseAddress + OFS_UCBxRXBUF));
468 uint8_t EUSCI_B_I2C_masterReceiveMultiByteFinish (uint16_t baseAddress)
471 HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTXSTP;
474 while (
HWREG16(baseAddress + OFS_UCBxCTLW0) & UCTXSTP)
477 while (!(
HWREG16(baseAddress + OFS_UCBxIFG) & UCRXIFG)) ;
481 return (
HWREG16(baseAddress + OFS_UCBxRXBUF));
484 bool EUSCI_B_I2C_masterReceiveMultiByteFinishWithTimeout (uint16_t baseAddress,
489 uint32_t timeout2 = timeout;
492 HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTXSTP;
495 while ((
HWREG16(baseAddress + OFS_UCBxCTLW0) & UCTXSTP) && --timeout);
503 while ((!(
HWREG16(baseAddress + OFS_UCBxIFG) & UCRXIFG)) && --timeout2);
512 *txData = (
HWREG8(baseAddress + OFS_UCBxRXBUF));
517 void EUSCI_B_I2C_masterReceiveMultiByteStop (uint16_t baseAddress)
520 HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCTXSTP;
523 void EUSCI_B_I2C_enableMultiMasterMode(uint16_t baseAddress)
525 HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCSWRST;
526 HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCMM;
529 void EUSCI_B_I2C_disableMultiMasterMode(uint16_t baseAddress)
532 HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCSWRST;
533 HWREG16(baseAddress + OFS_UCBxCTLW0) &= ~UCMM;
536 uint8_t EUSCI_B_I2C_masterReceiveSingle (uint16_t baseAddress)
539 if(!(
HWREG16(baseAddress + OFS_UCBxIE) & UCRXIE0)) {
540 while(!(
HWREG16(baseAddress + OFS_UCBxIFG) & UCRXIFG0));
544 return (
HWREG16(baseAddress + OFS_UCBxRXBUF));
547 uint32_t EUSCI_B_I2C_getReceiveBufferAddress (uint16_t baseAddress)
549 return ( baseAddress + OFS_UCBxRXBUF );
552 uint32_t EUSCI_B_I2C_getTransmitBufferAddress (uint16_t baseAddress)
554 return ( baseAddress + OFS_UCBxTXBUF );
556 void EUSCI_B_I2C_setTimeout(uint16_t baseAddress, uint16_t timeout)
558 uint16_t tempUCBxCTLW0;
561 tempUCBxCTLW0 =
HWREG16(baseAddress + OFS_UCBxCTLW0);
563 HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCSWRST;
566 HWREG16(baseAddress + OFS_UCBxCTLW1) = (
HWREG16(baseAddress + OFS_UCBxCTLW1) & (~UCCLTO_3)) | timeout;
569 HWREG16(baseAddress + OFS_UCBxCTLW0) = tempUCBxCTLW0;
MPU_initThreeSegmentsParam param